package com.base.exp

import android.text.SpannableString
import android.text.Spanned
import android.text.TextUtils
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import androidx.core.graphics.toColorInt

fun CharSequence?.nullToDefault(defValue: String = ""): String {
    return if (null == this || TextUtils.isEmpty(this)) defValue else this.toString()
}

fun SpannableString.link(linkText: String, click: ClickableSpan): SpannableString {
    val index = indexOf(linkText)
    if (-1 == index)
        return this
    val end = index + linkText.length
    val textColor = "#3F51B5".toColorInt()
    setSpan(
        UnderlineSpan(),
        index, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    setSpan(click, index, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    setSpan(
        ForegroundColorSpan(textColor), index,
        end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    return this
}